package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dao.CategoryMapper;
import com.itheima.reggie.domian.Category;
import com.itheima.reggie.domian.Dish;
import com.itheima.reggie.domian.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //判断该分类下菜品有没有关联
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int countdish = dishService.count(dishLambdaQueryWrapper);
        if (countdish>0){
            throw new CustomerException("该分类下有相关联菜品");
        }
        //判断该分类下菜品有没有关联
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int countsetmeal = setmealService.count(setmealLambdaQueryWrapper);
        if (countsetmeal>0){
            throw new CustomerException("该分类下有相关联套餐");
        }
        this.removeById(id);
    }
}
